//
//  XMAlbumSubCell.m
//  XiaoMiao
//
//  Created by 23 on 2018/1/28.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMAlbumSubCell.h"
#import "XMAlbumSubsubModel.h"
#import "XMAlbumJigsawAvatarCell.h"

static NSString *const kPhotoCollCellId = @"kPhotoCollCellId";
static NSString *const kCompareCollCellId = @"kCompareCollCellId";

@interface XMAlbumSubCell () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, XMAlbumJigsawAvatarCellDelegate, UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet UIButton *rowButton;
@property (weak, nonatomic) IBOutlet UIView *titleView;

@property (weak, nonatomic) IBOutlet UIView *albumContainView;
@property (weak, nonatomic) IBOutlet UICollectionView *albumCollView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *albumContainViewLayoutH;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tagButtonWLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tagButtonHLayout;

@end

@implementation XMAlbumSubCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.clipsToBounds = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentViewDidTap:)];
    tap.delegate = self;
    [self.titleView addGestureRecognizer:tap];
    
    [self setupUI];
}

#pragma mark - UI
- (void)setupUI
{ 
    self.albumCollView.delegate = self;
    self.albumCollView.dataSource = self;
//    [self.albumCollView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kPhotoCollCellId];
    [self.albumCollView registerNib:[UINib nibWithNibName:NSStringFromClass(XMAlbumJigsawAvatarCell.class) bundle:nil] forCellWithReuseIdentifier:[XMAlbumJigsawAvatarCell cellId]];
    [self.albumCollView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCompareCollCellId];
    
    //
    self.rowButton.app_touchExtendInset = UIEdgeInsetsMake(-10, -10, -10, -10);
    [self.rowButton addTarget:self action:@selector(rowButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setAlbumSubCellOperate:(XMAlbumSubCellOperate)albumSubCellOperate {
    _albumSubCellOperate = albumSubCellOperate;
    
    if (albumSubCellOperate == XMAlbumSubCellOperateUpload) {
        self.tagButtonWLayout.constant = 23.f;
        self.tagButtonHLayout.constant = 23.f;
        
        [self.rowButton setImage:[UIImage imageNamed:@"ic_album_unchoose_border"] forState:UIControlStateNormal];
        [self.rowButton setImage:[UIImage imageNamed:@"ic_album_choosed"] forState:UIControlStateSelected];
    }else {
        self.tagButtonWLayout.constant = 20.f;
        self.tagButtonHLayout.constant = 20.f;
        
        [self.rowButton setImage:[UIImage imageNamed:@"ic_ablum_right_arrow"] forState:UIControlStateNormal];
        [self.rowButton setImage:[UIImage imageNamed:@"ic_ablum_down_arrow"] forState:UIControlStateSelected];
    }
}
 

- (void)setItem:(XMAlbumSubsubModel *)item
{
    _item = item;
    self.titleLabel.text = item.subCateName;
    self.detailLabel.text = item.subSubCateName;
    if (self.albumSubCellOperate == XMAlbumSubCellOperateUpload) {
        self.timeLabel.text = @"";
        
//        self.albumContainViewLayoutH.constant = [XMAlbumSubCell topViewH];
        
    }else {
        self.timeLabel.text = [item.createTime.description app_formatTimeString:@"YYYY-MM-dd"];
        
        switch (self.albumType) {
            case XMAlbumSubTypePhotos: {
                self.albumContainViewLayoutH.constant = item.photoAlbumViewH;
                NSLog(@"------>%@%f", item.subSubCateName, item.photoAlbumViewH);
            }
                break;
                
            case XMAlbumSubTypeCompare: {
                self.albumContainViewLayoutH.constant = item.compareAlbumViewH;
                NSLog(@"------>%@%f", item.subSubCateName, item.compareAlbumViewH);
            }
                break;
        }
    }
    self.rowButton.selected = item.isFoldAlubm;
    
    [self.albumCollView reloadData];

    
    [self layoutSubviews];
    [self layoutIfNeeded];
    
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.item.photographUserAngleRefViews.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    switch (self.albumType) {
        case XMAlbumSubTypePhotos: {
//            cell = [collectionView dequeueReusableCellWithReuseIdentifier:kPhotoCollCellId forIndexPath:indexPath];
//
//            cell.contentView.backgroundColor = kRGBColor(200, 200, 200);
//
//            UIImageView *headImageView = [[UIImageView alloc] initWithFrame:cell.bounds];
//            [headImageView sd_setImageWithURL:[NSURL URLWithString: self.item.photographUserAngleRefViews[indexPath.row].imageUrl] placeholderImage:nil];
//            [cell.contentView addSubview:headImageView];
            XMAlbumJigsawAvatarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[XMAlbumJigsawAvatarCell cellId] forIndexPath:indexPath];
            cell.delegate = self;
            
            switch (self.albumSubCellOperate) {
                case XMAlbumSubCellOperateNone: 
                case XMAlbumSubCellOperateUpload:
                    cell.albumImageType = AlbumImageTypeNone;
                    break;
                    
                case XMAlbumSubCellOperateJigsaw:
                    cell.albumImageType = AlbumImageTypeSeleted;
                    break;
                    
            }
            
            cell.albumImageModel = self.item.photographUserAngleRefViews[indexPath.row];
            
            return cell;
        }
            break;
            
        case XMAlbumSubTypeCompare: {
            UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCompareCollCellId forIndexPath:indexPath];
            
            cell.contentView.backgroundColor = kRGBColor(200, 200, 200);
            
            [cell.contentView removeAllSubviews];
            
            UIView *imgConView = [[UIView alloc] initWithFrame:cell.bounds];
            [cell.contentView addSubview:imgConView];
            
            UIImageView *originHeadImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.app_width * 0.5, cell.app_height)];
            [originHeadImageView sd_setImageWithURL:[NSURL URLWithString: self.item.photographUserAngleRefViews[indexPath.row].originalUrl] placeholderImage:nil];
            [imgConView addSubview:originHeadImageView];
            
            UIImageView *myHeadImageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.app_width * 0.5, 0, cell.app_width * 0.5, cell.app_height)];
            [myHeadImageView sd_setImageWithURL:[NSURL URLWithString: self.item.photographUserAngleRefViews[indexPath.row].imageUrl] placeholderImage:nil];
            [imgConView addSubview:myHeadImageView];
            
            
            return cell;
        }
            break;
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AlbumImageModel *albumImageModel = self.item.photographUserAngleRefViews[indexPath.row];
    
    if (albumImageModel.titleStr) {
        
        if ([albumImageModel.titleStr isEqualToString:@"查看备注"]) {
            
            if (self.remarkBlock) {
                self.remarkBlock();
            }
        }else if ([albumImageModel.titleStr isEqualToString:@"删除"]){
            if (self.deleteBlock) {
                self.deleteBlock();
            }
            
        }
        
    }else{
        if (self.albumSubCellOperate != XMAlbumSubCellOperateJigsaw) {
            if ([self.delegate respondsToSelector:@selector(albumSubCell:didScanImageModel:)]) {
                [self.delegate albumSubCell:self didScanImageModel:albumImageModel];
            }
            return;
        }
        

        albumImageModel.isSeleted = !albumImageModel.isSeleted;
        
        [collectionView reloadData];
        
        if ([self.delegate respondsToSelector:@selector(albumSubCell:didSeletedAlbumImageModel:)]) {
            [self.delegate albumSubCell:self didSeletedAlbumImageModel:albumImageModel];
        }
    }
    

}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (self.albumType) {
        case XMAlbumSubTypePhotos: {
            CGFloat w = (kScreenWidth - (10 * 2) - (5 * 3)) * 1.0 / 4.0;
            return CGSizeMake(w, w);
        }
            break;
            
        case XMAlbumSubTypeCompare: {
            CGFloat w = (kScreenWidth - (10 * 2) - 5) * 1.0 / 2.0;
            return CGSizeMake(w, w * 0.5);
        }
            break; 
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5.f;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5.f;
}

#pragma mark - XMAlbumJigsawAvatarCellDelegate
//TODO:LLQ

#pragma mark - action
- (void)rowButtonDidClick:(UIButton *)sender
{
    if (![self.delegate respondsToSelector:@selector(albumSubCell:didCellFold:item:)]) {
        return;
    }
    
    sender.selected = !sender.isSelected;
    
    [self.delegate albumSubCell:self didCellFold:sender.isSelected item:self.item];
}

- (void)contentViewDidTap:(UITapGestureRecognizer *)tap
{
    if (![self.delegate respondsToSelector:@selector(albumSubCell:didCellFold:item:)]) {
        return;
    }
    
    self.rowButton.selected = !self.rowButton.isSelected;
    
    [self.delegate albumSubCell:self didCellFold:self.rowButton.isSelected item:self.item];
}


 
#pragma mark - Class public
+ (CGFloat)topViewH {
    return 75.f;
}
@end
